package com.luck.pf.init;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.luck.pf.common.Constants;
import com.luck.pf.util.ConfigUtil;

/**
 * 说明: 初始化参数
 * Copyright: Copyright (c) 2010
 * @author zhangwenrui 2010-9-19 上午11:09:53
 * @version 1.0
 */
public class SystemInitServlet extends HttpServlet {
	private static final long serialVersionUID = 2021728242995145241L;
	public final static Logger log = Logger.getLogger(SystemInitServlet.class);
	public static ConfigUtil config;
	public SystemInitServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
	}

	public void init() throws ServletException {
		log.info("=================================");
    	String wwwroot = this.getClass().getClassLoader().getResource("/").getPath();
        if(wwwroot.indexOf("WEB-INF")>-1 || wwwroot.indexOf("classes")>-1)
        	wwwroot = wwwroot.replaceAll("WEB-INF/classes/", "");
        Constants.WEB_ROOT=wwwroot;
        try {
        	initConfig();
        	log.info("初始化环境对象  成功!");
		} catch (Exception e) {
			log.info("初始化环境对象  失败!");
		}
        readWebSysConfigInfo();
        log.info("初始化项目系统配置信息 完成");
        log.info((new StringBuilder("WEB系统根目录:")).append(wwwroot).toString());
		log.info("=================================");
	}

	public static void readWebSysConfigInfo() {
		Constants.PROJECT_NAME = config.get("config/project_name", "未配置项目名称");
		Constants.COPYRIGHT = config.get("config/copyright", "未配置版权信息");
		Constants.VERSION = config.get("config/version", "未配置版本信息");
		Constants.ROOT_FLAG = config.get("config/root_flag", "ROOT");//如果为空，则默认显示系统全功功能菜单ROOT
		//登录验证码
        String vercode = config.get("config/vercode_check", "1");
        if("1".equals(vercode)){
        	Constants.VERCODE_CHECK = true;
        } 
	}

	 public static void initConfig() throws Exception{
	        try{
	            String congfigFileName = ("config/portal.xml");
	            config = new ConfigUtil(congfigFileName);
	            System.out.println("Config file load OK!");
	        }catch(IOException ex) {
	            ex.printStackTrace();
	            throw ex;
	        }
	    }
}
